<div class="fsh-rightPanel">
    <div class="layui-anim layui-anim-upbit">
        <div class="layui-form-item wx-search" id="list_form">
<!--            <div class="layui-inline">
                <input type="text" name="inputcode" placeholder="请输入关键字" autocomplete="off" class="layui-input">
            </div>
            <button class="layui-btn btn-primary" id="search">搜索</button>-->
            <div class="layui-inline right">
                <button class="layui-btn btn-primary" id="msg_add">新建活动</button>
                <!--<button class="layui-btn btn-danger" id="msg_del">批量删除</button>-->
            </div>
        </div>
        <table id="list_table" class="layui-hide" lay-filter="mainList"></table>
    </div>
</div>

<script>
    layui.use(['layer', 'table'], function () {
        var layer = layui.layer;
        var table = layui.table;
        var tableObj = table.render({
            id: 'list_table',
            elem: '#list_table',
            url: '/zhuli/info/list',
            align: "center",
            cols: [[ //表头
                {type: 'checkbox'},
                {field: 'baseCode', title: '活动编码', width: 100, align: 'center',},
                {field: 'baseTitle', title: '标题', width: 200, align: 'center',},
                {field: 'baseStartTime', title: '开始时间', width: 170, align: 'center',},
                {field: 'baseEndTime', title: '结束时间', width: 170, align: 'center',},
                {
                    field: 'baseStatus', title: '状态', width: 150, align: 'center', templet: function (d) {
                        return d.baseStatus == 0 ? '有效' : '无效';
                    }
                },
                {field: 'baseQrcodeValue', title: '活动二维码', width: 150, align: 'center',},
                {
                    field: 'lock', title: '操作', width: 300, align: 'center', templet: function (d) {
                        return '<a class="font-primary2" lay-event="edit" href="javascript:;">编辑</a>' +
                            '<a class="font-primary" lay-event="send" data-url="/activity/send/list[baseCode=' + d.baseCode + ']" href="javascript:;">发起列表</a>' +
                            '<a class="font-primary" lay-event="data" href="javascript:;">活动数据</a>' +
                            '<a class="font-primary" lay-event="mass" data-url="/activity/redeem/list[baseCode=' + d.baseCode + ']" href="javascript:;">活动券码</a>';
                    }, unresize: true, align: 'center'
                }
            ]],
        });

        $("#search").click(function () {
            reloadTable(tableObj);
        });

        // 添加
        $("#msg_add").click(function () {
            showDialog({
                title: '添加'
                , template: 'add'
                , saveUrl: '/msgtext/updateText'
                , tableObj: tableObj
            })
        });

        // 删除
        $("#msg_del").click(function () {
            var data = table.checkStatus('list_table').data;//已选中数据
            if (data.length == 0) {
                layer.msg("至少选择一条");
                return;
            }
            var arr = [];     //选中数组
            for (var i = 0; i < data.length; i++) {
                arr.push(data[i].baseId)
            }
            showConfirm("确认删除？", function () {
                $.ajax({
                    url: '/msgtext/deleteById',
                    data: {"baseId": arr.join(",")},
                    success: function (result) {
                        if (result.success) {
                            layer.msg("删除成功");
                            reloadTable(tableObj);
                        } else {
                            layer.msg("删除失败");
                        }
                    }
                })
            });
        });

        //表格内部操作按钮监听
        table.on('tool(mainList)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent == 'data') {
                $.ajax({
                    url: '/zhuli/info/data',
                    data: {baseCode: data.baseCode},
                    success: function (result) {
                        if (result.success) {
                            showDialog({
                                title: '活动数据'
                                , template: 'data'
                                , saveUrl: ''
                                , width: 700
                                , height: 410
                                , htmlData: result.data
                                , btn: ["关闭"]
                                , yes: function (index, layero) {
                                    layer.close(index);
                                }
                            })
                        }
                    },
                    error: function () {
                        layer.msg("同步异常");
                    }
                })
            } else if (layEvent === 'edit') {
                showDialog({
                    title: '修改',
                    template: 'edit',
                    saveUrl: '/msgtext/updateText',
                    tableObj: tableObj,
                    htmlData: data,
                })
            } else if (layEvent === 'mass') {
                showDialog({
                    title: '群发',
                    template: '/views/common/template/fans-chose.html',
                    height: 600,
                    yes: function (index) {
                        if (GLOBAL.choosed.length < 2) {
                            layer.msg("至少选择2个粉丝");
                            return false;
                        }
                        $.ajax({
                            url: '/wxapi/massSendTextByOpenIds',
                            data: {
                                textId: data.id,
                                openIds: GLOBAL.choosed.join(",")
                            },
                            success: function (result) {
                                if (result == "1") {
                                    layer.msg("群发成功");
                                    reloadTable(tableObj);
                                    layer.close(index);
                                } else {
                                    layer.msg("群发失败");
                                }
                            }
                        })
                    },
                    end: function () {
                        GLOBAL.choosed = [];
                    }
                });
            }
        });
    });
</script>
